/*
 * Daniel R Padilla
 *
 * Copyright (c) 2008, Daniel R Padilla
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.databasefacade.util;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;

/**
 * The class reads a directory and creates
 * a file listing for all files that are of
 * type specified in the filter.
 * 
 * @author Daniel
 *
 */
public class FilteredFileList {
	ArrayList fileList = new ArrayList();
	private String filter = null;
	
	/**
	 * Set the filter to lower case since case 
	 * should not matter from the file system.
	 * 
	 * @param directory
	 * @param filter
	 */
    public FilteredFileList(String directory, String filter) {
        this.filter = filter.toLowerCase();
        fillFilteredFileList(directory);
    }
    
    public FilteredFileList(File file, String filter) {
        this.filter = filter.toLowerCase();
    	if ( file.isDirectory() ) {
            fillFilteredFileList(file.getPath());
    	} else {
    		String fileName = file.getPath();
    		if ( fileName.toString().toLowerCase().endsWith(this.filter) ) {
    			fileList.add(file);
    		}
    	}
    }
        
    public File get(int i) {
    	return (File) fileList.get(i);
    }
    
    public void add(File file) {
    	fileList.add(file);
    }
    
    public int size() {
    	return fileList.size();
    }
    
    /**
     * Obtain all the *.xml files in a directory.
     * 
     * @param directory
     * @return
     */
    private void fillFilteredFileList(String directory) {
    	fileList = new ArrayList();
        String[] directories = new String[1];
        directories[0] = directory;
        ArrayList objectFileArray = listFilesRecursively(directories,
                fileFilter);
        for (int i = 0; i < objectFileArray.size(); i++) {
            File f = (File) objectFileArray.get(i);
            add(f);
        } 
    }
    
    /**
     * Method for obtaining XML files recursively. The
     * method uses a filter to recognize which files
     * are xml files.
     * 
     * @param directories
     * @param filter
     * @return ArrayList
     */
    private ArrayList listFilesRecursively(String[] directories, final FileFilter filter) {
        ArrayList list = new ArrayList();
        for (int i = 0; i < directories.length; i++) {
            String directory = directories[i];
            FileFilter filter2 = new FileFilter() {
                public boolean accept(File file) {
                    return file.isDirectory() || filter.accept(file);
                }
            };
            getFiles(getFile(directory), list, filter2);
        }
        return list;
    }   

    /**
     * Get the files so that they can be inspected and
     * added to the list.
     * 
     * @param directory
     * @param list
     * @param filter
     */
    private void getFiles(File directory, ArrayList list, FileFilter filter) {
        File[] files = directory.listFiles(filter);
        if (files == null) {
            return;
        }
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                getFiles(files[i], list, filter);
            } else {
                list.add(files[i]);
            }
        }
    }

    /**
     * File get utility method
     * 
     * @param name
     * @return File
     */
    private File getFile(final String name) {
        return new File(name);
    }
    
    /**
     * Filter setup method
     */
    private FileFilter fileFilter = new FileFilter() {
        public boolean accept(File file) {
            return file.toString().toLowerCase().endsWith(filter);
        }
    };   


}
